package com.media.selector.sample

import android.app.Application
import android.graphics.Bitmap
import android.net.Uri
import android.widget.ImageView
import com.bumptech.glide.load.DecodeFormat
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.media.selector.MediaSelector
import com.media.selector.media.loader.IMediaSelecterLoader
import com.module.imageloader.ImageLoader

/**
 * date        ：2020/5/19
 * author      ：蒙景博
 * description ：
 */
class AppContext: Application() {

    override fun onCreate() {
        super.onCreate()
        val mItemSize = resources.displayMetrics.widthPixels / 4
        MediaSelector.with().init(object : IMediaSelecterLoader {

            override fun loadThumbnail(uri: Uri?, imageView: ImageView) {
                ImageLoader.getDefault().with(this@AppContext)
                    .load(uri ?: R.drawable.draw_media_placeholder)
                    .fallbackHolder(R.drawable.draw_media_placeholder)
                    .placeHolder(R.drawable.draw_media_placeholder)
                    .errorHolder(R.drawable.draw_media_placeholder)
                    .preload(true)
                    .thumbnail(0.8f)
                    .override(mItemSize, mItemSize)
                    .compressFormat(Bitmap.CompressFormat.WEBP)
                    .decodeFormat(DecodeFormat.PREFER_RGB_565)
                    .skipMemoryCache(true)
                    .diskCacheStrategy(DiskCacheStrategy.NONE)
                    .build()
                    .into(imageView)
            }

            override fun loadImage(uri: Uri?, imageView: ImageView) {
                ImageLoader.getDefault().with(this@AppContext)
                    .load(uri ?: R.drawable.draw_media_placeholder)
                    .fallbackHolder(R.drawable.draw_media_placeholder)
                    .placeHolder(R.drawable.draw_media_placeholder)
                    .errorHolder(R.drawable.draw_media_placeholder)
                    .preload(true)
                    .compressFormat(Bitmap.CompressFormat.WEBP)
                    .decodeFormat(DecodeFormat.PREFER_ARGB_8888)
                    .skipMemoryCache(true)
                    .diskCacheStrategy(DiskCacheStrategy.NONE)
                    .build()
                    .into(imageView)
            }
        })
    }

    override fun onLowMemory() {
        super.onLowMemory()
        ImageLoader.getDefault().clearMemory(this)
    }

    override fun onTrimMemory(level: Int) {
        super.onTrimMemory(level)
        ImageLoader.getDefault().trimMemory(this, level)
    }
}